package com.agent.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.agent.exception.MyException;
import com.agent.model.Agent;
import com.agent.service.IAgentSer;

@Controller
@RequestMapping(value="/agent",produces="text/html;charset=UTF-8")
public class AgentController extends BaseController {
	@Autowired
	IAgentSer agentSer;
	
	/**
	 * 添加代理
	 * @param model
	 * @param request
	 * @param agent
	 * @return
	 */
	@RequestMapping("/addAgent")
	@ResponseBody
	public String addAgent(Model model, HttpServletRequest request,Agent agent){
		Agent loginAgent = (Agent) request.getSession().getAttribute("agent");
		Integer level=loginAgent.getLevel();
		Integer agentid= loginAgent.getAgentid();
		try {
			int result = agentSer.addAgent(loginAgent, level, agentid);
			return super.ajaxReturnTool.success(result).toJson();
		} catch (MyException e) {
			return super.ajaxReturnTool.fail(e.getCode()).toJson();
		}
	}
	
	/**
	 * 代理登录
	 * @param model
	 * @param request
	 * @param agent
	 * @return
	 */
	@RequestMapping("/loginAgent")
	@ResponseBody
	public String loginAgent(Model model, HttpServletRequest request,Agent agent){
		try {
			Agent result = agentSer.loginAgent(agent);
			request.getSession().setAttribute("agent", result);
			return super.ajaxReturnTool.success(result).toJson();
		} catch (MyException e) {
			return super.ajaxReturnTool.fail(e.getCode()).toJson();
		}
	}
}
